/*
 * Copyright (C) 2017 WordPlat Open Source Project
 *
 *      https://wordplat.com/InteractiveKLineView/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.ikvstockchart.compat;


import com.wordplat.ikvstockchart.align.XMarkerAlign;
import com.wordplat.ikvstockchart.align.YLabelAlign;
import com.wordplat.ikvstockchart.align.YMarkerAlign;
import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.ikvstockchart.entry.EntrySet;
import com.wordplat.ikvstockchart.entry.SizeColor;
import com.wordplat.ikvstockchart.utils.TypedAttrUtils;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

import static ohos.agp.components.AttrHelper.getDensity;

/**
 * <p>ViewUtils</p>
 * <p>Date: 2017/3/29</p>
 *
 * @author
 * @since 2021-05-09
 */
public class ViewUtils {

    private ViewUtils() {

    }

    /**
     * 根据手机的分辨率从 vp 的单位 转成为 px(像素)
     *
     * @param context context
     * @param vp      vp
     * @return int
     */
    public static int vpToPx(final Context context, final float vp) {
        return AttrHelper.vp2px(vp, getDensity(context));
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 vp
     *
     * @param context context
     * @param px      px
     * @return pxTovp
     */
    public static int pxTovp(final Context context, final float px) {
        return Math.round(px / getDensity(context) + 0.5f);
    }

    /**
     * 设置 蜡烛图画笔的颜色和是否空心
     *
     * @param candlePaint       candlePaint
     * @param entrySet          candlePaint
     * @param currentEntryIndex candlePaint
     * @param sizeColor         candlePaint
     * @return Entry
     */
    public static Entry setUpCandlePaint(Paint candlePaint, EntrySet entrySet
            , int currentEntryIndex, SizeColor sizeColor) {
        Entry entry = entrySet.getEntryList().get(currentEntryIndex);

        // 设置 涨、跌的颜色
        if (entry.getClose() > entry.getOpen()) { // 今日收盘价大于今日开盘价为涨
            candlePaint.setColor(new Color(sizeColor.getIncreasingColor()));
        } else if (entry.getClose() == entry.getOpen()) { // 今日收盘价等于今日开盘价有涨停、跌停、不涨不跌三种情况
            if (currentEntryIndex > 0) {
                if (entry.getOpen() > entrySet.getEntryList().get(currentEntryIndex - 1).getClose()) { // 今日开盘价大于昨日收盘价为涨停
                    candlePaint.setColor(new Color(sizeColor.getIncreasingColor()));
                } else if (entry.getOpen() == entrySet.getEntryList().get(currentEntryIndex - 1).getClose()) { // 不涨不跌
                    candlePaint.setColor(new Color(sizeColor.getNeutralColor()));
                } else { // 否则为跌停
                    candlePaint.setColor(new Color(sizeColor.getDecreasingColor()));
                }
            } else {
                if (entry.getOpen() > entrySet.getPreClose()) {
                    candlePaint.setColor(new Color(sizeColor.getIncreasingColor()));
                } else if (entry.getOpen() == entrySet.getPreClose()) {
                    candlePaint.setColor(new Color(sizeColor.getNeutralColor()));
                } else {
                    candlePaint.setColor(new Color(sizeColor.getDecreasingColor()));
                }
            }
        } else { // 今日收盘价小于今日开盘价为跌
            candlePaint.setColor(new Color(sizeColor.getDecreasingColor()));
        }

        if (candlePaint.getColor().getValue() == sizeColor.getIncreasingColor()) {
            if (sizeColor.getIncreasingStyle() == Paint.Style.STROKE_STYLE) {
                candlePaint.setStyle(Paint.Style.STROKE_STYLE);
            } else {
                candlePaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
            }
        } else {
            if (sizeColor.getDecreasingStyle() == Paint.Style.STROKE_STYLE) {
                candlePaint.setStyle(Paint.Style.STROKE_STYLE);
            } else {
                candlePaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
            }
        }

        return entry;
    }

    /**
     * 初始化 SizeColor
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr defStyleAttr
     * @return SizeColor
     */
    public static SizeColor getSizeColor(Context context, AttrSet attrs, int defStyleAttr) {

        final SizeColor sizeColor = new SizeColor();

        try {
            /**
             * 与轴、网格有关的属性
             */
            sizeColor.setXLabelSize(TypedAttrUtils.getFloat(attrs, "xLabelSize", sizeColor.getXLabelSize()));
            sizeColor.setXLabelColor(TypedAttrUtils.getIntColor(attrs, "xLabelColor", sizeColor.getXLabelColor()));
            sizeColor.setXLabelViewHeight(TypedAttrUtils.getFloat(attrs, "xLabelViewHeight", sizeColor.getXLabelViewHeight()));
            sizeColor.setYLabelSize(TypedAttrUtils.getFloat(attrs, "yLabelSize", sizeColor.getYLabelSize()));
            sizeColor.setYLabelColor(TypedAttrUtils.getIntColor(attrs, "yLabelColor", sizeColor.getYLabelColor()));

            String align = TypedAttrUtils.getString(attrs, "yLabelAlign", YLabelAlign.LEFT.toString());
            align = align.toUpperCase();
            int alignIndex = 0;
            if (align.equals(YLabelAlign.LEFT.toString())) {
                alignIndex = 0;
            } else if (align.equals(YLabelAlign.RIGHT.toString())) {
                alignIndex = 1;
            }

            sizeColor.setYLabelAlign(YLabelAlign.values()[alignIndex]);
            sizeColor.setAxisSize(TypedAttrUtils.getFloat(attrs, "axisSize", sizeColor.getAxisSize()));
            sizeColor.setAxisColor(TypedAttrUtils.getIntColor(attrs, "axisColor", sizeColor.getAxisColor()));
            sizeColor.setGridSize(TypedAttrUtils.getFloat(attrs, "gridSize", sizeColor.getGridSize()));
            sizeColor.setGridColor(TypedAttrUtils.getIntColor(attrs, "gridColor", sizeColor.getGridColor()));

            /**
             * 与高亮、MarkerView 有关的属性
             */
            sizeColor.setHighlightSize(TypedAttrUtils.getFloat(attrs, "highlightSize", sizeColor.getHighlightSize()));
            sizeColor.setHighlightColor(TypedAttrUtils.getIntColor(attrs, "highlightColor", sizeColor.getHighlightColor()));
            sizeColor.setMarkerBorderSize(TypedAttrUtils.getFloat(attrs, "markerBorderSize", sizeColor.getMarkerBorderSize()));
            sizeColor.setMarkerBorderColor(TypedAttrUtils.getIntColor(attrs, "markerBorderColor", sizeColor.getMarkerBorderColor()));
            sizeColor.setMarkerTextSize(TypedAttrUtils.getFloat(attrs, "markerTextSize", sizeColor.getMarkerTextSize()));
            sizeColor.setMarkerTextColor(TypedAttrUtils.getIntColor(attrs, "markerTextColor", sizeColor.getMarkerTextColor()));

            align = TypedAttrUtils.getString(attrs, "xMarkerAlign", XMarkerAlign.AUTO.toString());
            align = align.toUpperCase();
            if (align.equals(XMarkerAlign.TOP.toString())) {
                alignIndex = 0;
            } else if (align.equals(XMarkerAlign.BOTTOM.toString())) {
                alignIndex = 1;
            } else if (align.equals(XMarkerAlign.AUTO.toString())) {
                alignIndex = 2;
            }
            sizeColor.setXMarkerAlign(XMarkerAlign.values()[alignIndex]);

            align = TypedAttrUtils.getString(attrs, "yMarkerAlign", YMarkerAlign.AUTO.toString());
            align = align.toUpperCase();
            if (align.equals(YMarkerAlign.LEFT.toString())) {
                alignIndex = 0;
            } else if (align.equals(YMarkerAlign.RIGHT.toString())) {
                alignIndex = 1;
            } else if (align.equals(YMarkerAlign.AUTO.toString())) {
                alignIndex = 2;
            }
            sizeColor.setYMarkerAlign(YMarkerAlign.values()[alignIndex]);

            /**
             * 与分时图有关的属性
             */
            sizeColor.setTimeLineSize(TypedAttrUtils.getFloat(attrs, "timeLineSize", sizeColor.getTimeLineSize()));
            sizeColor.setTimeLineColor(TypedAttrUtils.getIntColor(attrs, "timeLineColor", sizeColor.getTimeLineColor()));
            sizeColor.setTimeLineMaxCount(TypedAttrUtils.getInteger(attrs, "timeLineMaxCount", sizeColor.getTimeLineMaxCount()));

            /**
             * 与蜡烛图有关的属性
             */
            sizeColor.setCandleBorderSize(TypedAttrUtils.getFloat(attrs, "candleBorderSize", sizeColor.getCandleBorderSize()));
            sizeColor.setCandleExtremumLabelSize(TypedAttrUtils.getFloat(attrs, "candleExtremumLabelSize", sizeColor.getCandleExtremumLabelSize()));
            sizeColor.setCandleExtremumLableColor(TypedAttrUtils.getIntColor(attrs, "candleExtremumLableColor", sizeColor.getCandleExtremumLableColor()));
            sizeColor.setShadowSize(TypedAttrUtils.getFloat(attrs, "shadowSize", sizeColor.getShadowSize()));
            sizeColor.setIncreasingColor(TypedAttrUtils.getIntColor(attrs, "increasingColor", sizeColor.getIncreasingColor()));
            sizeColor.setDecreasingColor(TypedAttrUtils.getIntColor(attrs, "decreasingColor", sizeColor.getDecreasingColor()));
            sizeColor.setNeutralColor(TypedAttrUtils.getIntColor(attrs, "neutralColor", sizeColor.getNeutralColor()));
            sizeColor.setPortraitDefaultVisibleCount(TypedAttrUtils.getInteger(attrs, "portraitDefaultVisibleCount", sizeColor.getPortraitDefaultVisibleCount()));
            sizeColor.setZoomInTimes(TypedAttrUtils.getInteger(attrs, "zoomInTimes", sizeColor.getZoomInTimes()));
            sizeColor.setZoomOutTimes(TypedAttrUtils.getInteger(attrs, "zoomOutTimes", sizeColor.getZoomOutTimes()));

            align = TypedAttrUtils.getString(attrs, "increasingStyle", "fill");
            if ("fill".equals(align)) {
                alignIndex = 0;
            } else if ("stroke".equals(align)) {
                alignIndex = 1;
            }
            sizeColor.setIncreasingStyle(Paint.Style.values()[alignIndex]);

            align = TypedAttrUtils.getString(attrs, "decreasingStyle", "fill");
            if ("fill".equals(align)) {
                alignIndex = 0;
            } else if ("stroke".equals(align)) {
                alignIndex = 1;
            }
            sizeColor.setDecreasingStyle(Paint.Style.values()[alignIndex]);

            /**
             * 与股票指标有关的属性
             */
            sizeColor.setMaLineSize(TypedAttrUtils.getFloat(attrs, "maLineSize", sizeColor.getMaLineSize()));
            sizeColor.setMa5Color(TypedAttrUtils.getIntColor(attrs, "ma5Color", sizeColor.getMa5Color()));
            sizeColor.setMa10Color(TypedAttrUtils.getIntColor(attrs, "ma10Color", sizeColor.getMa10Color()));
            sizeColor.setMa20Color(TypedAttrUtils.getIntColor(attrs, "ma20Color", sizeColor.getMa20Color()));
            sizeColor.setBollLineSize(TypedAttrUtils.getFloat(attrs, "bollLineSize", sizeColor.getBollLineSize()));
            sizeColor.setBollMidLineColor(TypedAttrUtils.getIntColor(attrs, "bollMidLineColor", sizeColor.getBollMidLineColor()));
            sizeColor.setBollUpperLineColor(TypedAttrUtils.getIntColor(attrs, "bollUpperLineColor", sizeColor.getBollUpperLineColor()));
            sizeColor.setBollLowerLineColor(TypedAttrUtils.getIntColor(attrs, "bollLowerLineColor", sizeColor.getBollLowerLineColor()));
            sizeColor.setKdjLineSize(TypedAttrUtils.getFloat(attrs, "kdjLineSize", sizeColor.getKdjLineSize()));
            sizeColor.setKdjKLineColor(TypedAttrUtils.getIntColor(attrs, "kdjKLineColor", sizeColor.getKdjKLineColor()));
            sizeColor.setKdjDLineColor(TypedAttrUtils.getIntColor(attrs, "kdjDLineColor", sizeColor.getKdjDLineColor()));
            sizeColor.setKdjJLineColor(TypedAttrUtils.getIntColor(attrs, "kdjJLineColor", sizeColor.getKdjJLineColor()));
            sizeColor.setMacdLineSize(TypedAttrUtils.getFloat(attrs, "macdLineSize", sizeColor.getMacdLineSize()));
            sizeColor.setMacdHighlightTextColor(TypedAttrUtils.getIntColor(attrs, "macdHighlightTextColor", sizeColor.getMacdHighlightTextColor()));
            sizeColor.setDeaLineColor(TypedAttrUtils.getIntColor(attrs, "deaLineColor", sizeColor.getDeaLineColor()));
            sizeColor.setDiffLineColor(TypedAttrUtils.getIntColor(attrs, "diffLineColor", sizeColor.getDiffLineColor()));
            sizeColor.setRsiLineSize(TypedAttrUtils.getFloat(attrs, "rsiLineSize", sizeColor.getRsiLineSize()));
            sizeColor.setRsi1LineColor(TypedAttrUtils.getIntColor(attrs, "rsi1LineColor", sizeColor.getRsi1LineColor()));
            sizeColor.setRsi2LineColor(TypedAttrUtils.getIntColor(attrs, "rsi2LineColor", sizeColor.getRsi2LineColor()));
            sizeColor.setRsi3LineColor(TypedAttrUtils.getIntColor(attrs, "rsi3LineColor", sizeColor.getRsi3LineColor()));

            sizeColor.setMaTextSize(TypedAttrUtils.getFloat(attrs, "maTextSize", sizeColor.getMaTextSize()));
            sizeColor.setMaTextColor(TypedAttrUtils.getIntColor(attrs, "maTextColor", sizeColor.getMaTextColor()));

            sizeColor.setBollTextSize(TypedAttrUtils.getFloat(attrs, "bollTextSize", sizeColor.getBollTextSize()));
            sizeColor.setBollTextColor(TypedAttrUtils.getIntColor(attrs, "bollTextColor", sizeColor.getBollTextColor()));

            sizeColor.setKdjTextSize(TypedAttrUtils.getFloat(attrs, "kdjTextSize", sizeColor.getKdjTextSize()));
            sizeColor.setKdjTextColor(TypedAttrUtils.getIntColor(attrs, "kdjTextColor", sizeColor.getKdjTextColor()));

            sizeColor.setMacdTextSize(TypedAttrUtils.getFloat(attrs, "macdTextSize", sizeColor.getMacdTextSize()));
            sizeColor.setMacdTextColor(TypedAttrUtils.getIntColor(attrs, "macdTextColor", sizeColor.getMacdTextColor()));

            sizeColor.setRsiTextSize(TypedAttrUtils.getFloat(attrs, "rsiTextSize", sizeColor.getRsiTextSize()));
            sizeColor.setRsiTextColor(TypedAttrUtils.getIntColor(attrs, "rsiTextColor", sizeColor.getRsiTextColor()));

            /**
             * 其它
             */
            sizeColor.setLoadingTextSize(TypedAttrUtils.getFloat(attrs, "loadingTextSize", sizeColor.getLoadingTextSize()));
            sizeColor.setLoadingTextColor(TypedAttrUtils.getIntColor(attrs, "loadingTextColor", sizeColor.getLoadingTextColor()));
            String loadingText = TypedAttrUtils.getString(attrs, "loadingText", "");
            if (!"".equals(loadingText)) {
                sizeColor.setLoadingText(loadingText);
            }

            sizeColor.setErrorTextSize(TypedAttrUtils.getFloat(attrs, "errorTextSize", sizeColor.getErrorTextSize()));
            sizeColor.setErrorTextColor(TypedAttrUtils.getIntColor(attrs, "errorTextColor", sizeColor.getErrorTextColor()));
            String errorText = TypedAttrUtils.getString(attrs, "errorText", "");
            if (!"".equals(errorText)) {
                sizeColor.setErrorText(errorText);
            }

        } finally {
        }
        return sizeColor;
    }
}
